using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using Utilities;
using BowMaster.SceneElements;

namespace BowMaster.Levels
{
    
    abstract class BaloonsTargetBase : ALevel
    {
        
        public Texture2D [] texture;
        public ArrayList<BaloonTarget> baloons;
        protected int objective, shooted;
        MyButton remainingBalloons_b, shooted_b;
        public BaloonsTargetBase(Game game, int index, String Name, int objective)
            : base(game, index, Name)
        {
            this.objective = objective;
            shooted = 0;
        }

        protected override void LoadContent()
        {
            texture = new Texture2D[]
            {
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon0"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon1"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon2"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon3"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon4"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon5"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon6"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon7"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon8"),
             Game.Content.Load<Texture2D>("Textures/balloon_pop/baloon9")
            };
            shooted = 0;
            baloons = new ArrayList<BaloonTarget>();
            base.LoadContent();
            updatePreview = true;
            remainingBalloons_b = new MyButton(new Rectangle(0, 20, 200, 50), Strings.RemainingBalloons, textColor, arial14);
            shooted_b = new MyButton(new Rectangle(0, 60, 200, 50), Strings.Current_Score + shooted, textColor, arial14);
            sceneRect = new Rectangle(0, 0, bar.X, bar.Height);
        }
        Rectangle sceneRect;
        public override void Update(GameTime gameTime)
        {
            if (!showHelp)
            {
                var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

                for (int i = 0; i < baloons.Count; i++)
                {
                    var b = baloons[i];
                    if (b.popped)
                    {
                        if (b.remove)
                        {
                            baloons.Remove(b);
                            i--;
                        }
                        b.Update(dt);
                        continue;
                    }
                    if (activeArrow != null)
                    {
                        
                        if (StaticUtilities.RectContainsVect(b.hitBoxes[0].box, activeArrow.point))
                        {
                            b.popped = true;
                            activeArrow = null;
                            showGetBackButton = true;
                            shoots++;
                            shooted++;
                            if (shooted>= objective)
                                LevelWon();
                        }
                    }
                    bool remove = false;
                    b.Update(dt);
                    var v = b.velocity;
                    if (v.X > 0 && b.hitBoxes[0].box.X > sceneRect.Width)
                        remove = true;
                    else if (v.X < 0 && b.hitBoxes[0].box.X + b.hitBoxes[0].box.Width < 0)
                        remove = true;
                    if (v.Y > 0 && b.hitBoxes[0].box.Y > sceneRect.Height)
                        remove = true;
                    else if (v.Y < 0 && b.hitBoxes[0].box.Y + b.hitBoxes[0].box.Height < 0)
                        remove = true;
                    if (remove)
                    {
                        baloons.Remove(b);
                        i--;
                    }
                }
                if (baloons.Count == 0)
                    LevelLost();
            }
            base.Update(gameTime);
        }
        public override void DrawLevelElements()
        {
            base.DrawLevelElements();
            for (int i = 0; i < baloons.Count; i++)
            {
                baloons[i].Draw(spriteBatch, view, projection);
                //baloons[i].DrawHitBoxes(spriteBatch, view, projection, whiteBox);
            }

        }
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

            remainingBalloons_b.text = Strings.RemainingBalloons + baloons.Count;
            shooted_b.text = Strings.Current_Score + shooted + " / " + objective;
            remainingBalloons_b.UpdateTextPos();
            shooted_b.UpdateTextPos();
            remainingBalloons_b.Draw(spriteBatch);
            shooted_b.Draw(spriteBatch);
            spriteBatch.End();
        }
        public void AddBalloon()
        {
            int y = rnd.Next(200, level_height - 480);
            int x=0;
            if(wind_level==0)
                x = rnd.Next(200,bar.X-50);
            else if (wind_level < 0)
                x = rnd.Next(400, level_width);
            else
                x = rnd.Next(-level_width/2, 300);
            baloons.Add(new BaloonTarget(texture, new Color(rnd.Next(255), rnd.Next(255), rnd.Next(255), 1), new Vector2(x, y), new Vector2(wind_level * rnd.Next(20, 50), 0)));
        }
    }
}
